--- /dev/null
+<!--
+
+Copyright (C) 2006 XenSource Inc.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of version 2.1 of the GNU Lesser General Public
+License as published by the Free Software Foundation.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+-->
+
+<!--
+
+This is a configuration file for xm; it should be placed in
+/etc/xen/xm-config.xml. If this file is missing, then xm will fall back to
+the normal behaviour that's in Xen 3.0.4 and below. The settings here are
+most useful for experimenting with the Xen-API preview in Xen 3.0.4.
+
+-->
+
+<xm>
+ <!-- The server element describes how to talk to Xend. The type may be
+ Xen-API or LegacyXMLRPC (the default). The URI is that of the
+ server; you might try http://server:9363/ or
+ httpu:///var/run/xend/xen-api.sock for the Xen-API, or
+ httpu:///var/run/xend/xmlrpc.sock for the legacy server.
+
+ The username and password attributes will be used to log in if Xen-API
+ is being used.
+ -->
+ <server type='Xen-API'
+ uri='http://localhost:9363/'
+ username='me'
+ password='mypassword' />
+</xm>
"""Grand unified management application for Xen.
"""
+import atexit
import os
import sys
import re
import traceback
import datetime
from select import select
+import xml.dom.minidom
import warnings
warnings.filterwarnings('ignore', category=FutureWarning)
from xen.xend import PrettyPrint
from xen.xend import sxp
from xen.xend import XendClient
-from xen.xend.XendClient import server
from xen.xend.XendConstants import *
from xen.xm.opts import OptionError, Opts, wrap, set_true
from xen.xm import console
from xen.util import security
+from xen.util.xmlrpclib2 import ServerProxy
+
+import XenAPI
# getopt.gnu_getopt is better, but only exists in Python 2.3+. Use
# getopt.getopt if gnu_getopt is not available. This will mean that options
if not hasattr(getopt, 'gnu_getopt'):
getopt.gnu_getopt = getopt.getopt
+XM_CONFIG_FILE = '/etc/xen/xm-config.xml'
+
+# Supported types of server
+SERVER_LEGACY_XMLRPC = 'LegacyXMLRPC'
+SERVER_XEN_API = 'Xen-API'
+
# General help message
USAGE_HELP = "Usage: xm <subcommand> [args]\n\n" \
all_commands = (domain_commands + host_commands + scheduler_commands +
device_commands + vnet_commands + acm_commands)
+
+##
+# Configuration File Parsing
+##
+
+if os.path.isfile(XM_CONFIG_FILE):
+ config = xml.dom.minidom.parse(XM_CONFIG_FILE)
+else:
+ config = None
+
+def parseServer():
+ if config:
+ server = config.getElementsByTagName('server')
+ if server:
+ st = server[0].getAttribute('type')
+ if st != SERVER_XEN_API:
+ st = SERVER_LEGACY_XMLRPC
+ return (st, server[0].getAttribute('uri'))
+
+ return SERVER_LEGACY_XMLRPC, XendClient.uri
+
+def parseAuthentication():
+ server = config.getElementsByTagName('server')[0]
+ return (server.getAttribute('username'),
+ server.getAttribute('password'))
+
+serverType, serverURI = parseServer()
+
+
####################################################################
#
# Help/usage printing functions
print >>sys.stderr, "Error:", msg
+def get_single_vm(dom):
+ uuids = server.xenapi.VM.get_by_name_label(dom)
+ n = len(uuids)
+ if n == 1:
+ return uuids[0]
+ else:
+ dominfo = server.xend.domain(dom, False)
+ return dominfo['uuid']
+
+
#########################################################################
#
# Main xm functions
sys.exit(1)
dom = params[0]
- server.xend.domain.start(dom, paused)
+ if serverType == SERVER_XEN_API:
+ server.xenapi.VM.start(get_single_vm(dom), paused)
+ else:
+ server.xend.domain.start(dom, paused)
def xm_delete(args):
arg_check(args, "delete", 1)
dom = args[0]
- server.xend.domain.delete(dom)
+ if serverType == SERVER_XEN_API:
+ server.xenapi.VM.destroy(dom)
+ else:
+ server.xend.domain.delete(dom)
def xm_suspend(args):
arg_check(args, "suspend", 1)
dom = args[0]
- server.xend.domain.suspend(dom)
+ if serverType == SERVER_XEN_API:
+ server.xenapi.VM.suspend(get_single_vm(dom))
+ else:
+ server.xend.domain.suspend(dom)
def xm_resume(args):
arg_check(args, "resume", 1, 2)
sys.exit(1)
dom = params[0]
- server.xend.domain.resume(dom, paused)
+ if serverType == SERVER_XEN_API:
+ server.xenapi.VM.resume(dom, paused)
+ else:
+ server.xend.domain.resume(dom, paused)
def xm_reboot(args):
arg_check(args, "reboot", 1, 3)
"Command %s is deprecated. Please use xm %s instead." % (old, new))
def main(argv=sys.argv):
+ global server
+
if len(argv) < 2:
usage()
args = argv[2:]
if cmd:
try:
+ if serverType == SERVER_XEN_API:
+ server = XenAPI.Session(serverURI)
+ username, password = parseAuthentication()
+ server.login_with_password(username, password)
+ def logout():
+ try:
+ server.xenapi.session.logout()
+ except:
+ pass
+ atexit.register(logout)
+ else:
+ server = ServerProxy(serverURI)
+
rc = cmd(args)
if rc:
usage()
sys.exit(1)
except SystemExit:
sys.exit(1)
+ except XenAPI.Failure, exn:
+ err(str(exn))
+ sys.exit(1)
except xmlrpclib.Fault, ex:
if ex.faultCode == XendClient.ERROR_INVALID_DOMAIN:
err("Domain '%s' does not exist." % ex.faultString)